﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;
using System.IO;

namespace LoLEdit
{
    public partial class frmOpenZipFile : Form
    {
        public frmOpenZipFile()
        {
            InitializeComponent();
        }

        private void frmOpenZipFile_Load(object sender, EventArgs e)
        {
            string m_LoLFolder = "C:\\Riot Games\\League of Legends";

            //Open the HeroPak
            ZipFile file = ZipFile.Read(m_LoLFolder + "\\game\\HeroPak_client.zip");

            //String strFilename = "DATA/Items/1018.inibin"; //"DATA/Characters/GeneralCharacterData.inibin";
            //String strFileType = "Item";

            treeFolderListing.Nodes.Clear();
            foreach (ZipEntry entry in file.Entries)
            {
                AddTreeNode(entry.FileName);
                //AddListItem(entry.FileName);
            }
        }


        /// <summary>
        /// Add a node to the Treeview, for the given ZipEntry name
        /// </summary>
        /// <param name="name">name of the ZipEntry</param>
        /// <returns>the TreeNode added</returns>
        private TreeNode AddTreeNode(String name)
        {
            if (name.EndsWith("/"))
            {
                name = name.Substring(0, name.Length - 1);
            }//end if

            TreeNode node = FindNodeForTag(name, treeFolderListing.Nodes);

            if (node != null)
            {
                return node;
            } //end if

            TreeNodeCollection pnodeCollection;
            String parent = Path.GetDirectoryName(name);

            if (parent == "")
            {
                pnodeCollection = treeFolderListing.Nodes;
            } //end if
            else
            {
                pnodeCollection = AddTreeNode(parent.Replace("\\", "/")).Nodes;
            } //end else

            node = new TreeNode();
            node.Text = Path.GetFileName(name);
            node.Tag = name; // full path
            pnodeCollection.Add(node);
            return node;
        }

        /// <summary>
        /// Returns the TreeNode for a given name 
        /// </summary>
        /// <param name="name">name of the ZipEntry</param>
        /// <param name="nodes">The TreeNodeCollection to search</param>
        /// <returns>the matching TreeNode, or nothing if none exists</returns>
        /// <remarks>
        /// This method is used by AddTreeNode() to find existing nodes.  
        /// </remarks>
        private TreeNode FindNodeForTag(String name, TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                if (name == node.Tag.ToString())
                {
                    return node;
                }
                else if (name.StartsWith(node.Tag + "/"))
                {
                    return FindNodeForTag(name, node.Nodes);
                }
            }

            return null;
        }

    }
}
